﻿layui.extend({
    unobtrusive: 'lib/unobtrusive'
}).define(['form', 'jquery', 'unobtrusive', 'layer','index','upload','element'], function (exports) {
    var form = layui.form,
        $ = layui.$,
        index = layui.index,
        upload = layui.upload,
        element=layui.element,
        layer = layui.layer;

    var pageName = layui.cache.module.pageName;

    var loadElem =
        $('<i class="layui-icon layui-icon-loading-1 layui-anim layui-anim-rotate layui-anim-loop"></i>');


    //登录页面
    if (pageName === 'login') {

        index.beforeSubmit('user-login');

        //登录完成回调
        window.loginComplete = function (res) { 
            index.handleCallbackResult(res,
                function(data) {
                    window.location.href = '/Admin/Manage';
                });
        }
    }

    //注册页面
    if (pageName === 'register') {

        index.beforeSubmit('user-register');

        //登录完成回调
        window.registerComplete = function (res) {
            index.afterSubmit(res,
                '#btn_Register',
                function (data) {
                    window.location.href = '/';
                });
        }
    }

    //登录密码修改页面
    if (pageName === 'updatePassword') {

        form.on('submit(update-password)',
            function (data) {

                var formData = data.field;
                if (formData.NewPassword !==formData.ConfirmPassword) {
                    layer.msg("两次密码输入不一致", { icon: 5, anim: 6, time: 2000 });
                    return false;
                }

                //禁用按钮点击效果
                var $btnElem = $(data.elem);
                $btnElem.prepend(loadElem);
                $btnElem.attr('disabled', true);
                $btnElem.addClass('layui-btn-disabled');

                //按钮设置了disabled后，无法提交表单，需要主动提交
                var $formElem = $(data.form);
                $formElem.submit();
            });

        //密码修改完成回调
        window.updatePasswordComplete = function (res) {
            module.afterSubmit(res,
                '#btn_UpdatePassword',
                function (data) {
                    top.window.location.href = '/Admin/AccountManage/SignOut';
                });
        }
    }

    //登录密码修改页面
    if (pageName === 'updateUserMsg') {

        //初始化用户信息
        index.jsonAjax('/Admin/AccountManage/UpdateUserMsgInit',
            function (data) {
 
                form.val('update-userMsg',
                    {
                        PhoneNumber: data.PhoneNumber,
                        Email: data.Email,
                        Sex: data.Sex.toString(),
                        RegisterTime: data.RegisterTime,
                        NickName:data.NickName
                    });
                if (data.HeadImgUrl) {
                    $('#img_HeadImg').attr('src', data.HeadImgUrl);
                }
            });

        //修改用户头像
        upload.render({
            elem: '#btn_UpdateUserHeadImg',
            url: '/Admin/AccountManage/UpdateUserHeadImg',
            done: function (data) { 
                $('#img_HeadImg').attr('src', data.data); 
            }
        });

        index.beforeSubmit('update-userMsg');

        //用户信息修改完成回调
        window.updateUserMsgComplete = function (res) {
            index.afterSubmit(res,
                '#btn_UpdateUserMsg',
                function (data) {
                    top.window.location.href = '/';
                });
        }
        
    }

    
    exports('accountManage', null);
})